<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
require_once (ROOT_PATH."xajax_core/xajax.inc.php");

include_once("class/upload.class.php");
include_once("class/files.class.php");
include_once("class/history.class.php");

$xajax = new xajax("xr.php");
$xajax->registerFunction("rejectFile");
$xajax->registerFunction("rejectPackage");
//$xajax->setFlag("debug",true);
//$xajax->setFlag("statusMessages",true);

if ($param['op']=='confirm') {
	
	foreach($param['fileid'] as $fileid) {
		$upload = new upload;
		$upload->id=$fileid;
		$loader->get($upload);

		$filelist = array();
		foreach ($upload->filesid as $fid) {
			$file = new files($upload->projectid);
			$file->id=$fid;
			$ret = $loader->get($file);
			if ($ret) {
				$setstatus = $param['statusid'];
				if ($param['statusid']=='auto') {
					$chis = new history($upload->projectid);
					$chis->fileid = $file->id;
					$ret = $loader->getBy($chis,$chis->getLast());
					debug($chis);					
					if ($ret) {
						if ($chis->statusid == FILE_STATUS_LOCKED) {
							$setstatus = FILE_STATUS_CHKPND;
						} else {
							$setstatus = FILE_STATUS_DONE;
						}
					} else {
						$setstatus = FILE_STATUS_CHKPND;
					}
				}
				$file->statusid = $setstatus;
				$file->userid = $CURRENT_USER->userid;
				$file->auto = 0;
				$file->comment = $param['comment'];
				$file->stamp=util::hashcode($file->id);
				$loader->update($file);
		
				$his = new history($upload->projectid);
				$his->statusid=$file->statusid;
				$his->fileid=$file->id;
				$his->userid=$CURRENT_USER->userid;
				$his->comment=$file->comment;
				$loader->save($his);
				
				array_push($filelist,$file);
			}
		}
		sendCommitedMail($upload->userid,$CURRENT_USER,$filelist,$upload->fname);

		unlink(UPLOAD_PATH."/".$upload->id);
		$loader->delete($upload);
	}
	$messages=$LANG['updated'];
} 

$upload = new upload;
if ($isadmin) {
	$list = $loader->loadList($upload);
} else {
	$list = $loader->loadListBy($upload,$upload->listByTeam($CURRENT_USER->teamid));	
}

$filelist = array();

foreach ($list as $obj) {
	$ar = array();
	foreach ($obj->filesid as $fid) {
		$file = new files($obj->projectid);
		$file->id=$fid;
		$loader->get($file);
		array_push($ar,$file);
	}
	$obj->filelist = $ar;
}

$tm->assign('script',$xajax->getJavascript('scripts'));
$statuslist = array (FILE_STATUS_CHKPND,FILE_STATUS_DONE);
$tm->assign("statuslist",$statuslist);
$tm->assign("list",$list);
$tm->assign("filelist",$filelist);
$content=$tm->fetch("filecheckin.tpl");

?>
